package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.BzGoods;
import com.ruoyi.system.mapper.BzCustomerMapper;
import com.ruoyi.system.domain.BzCustomer;
import com.ruoyi.system.service.IBzCustomerService;

/**
 * 客户Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-23
 */
@Service
public class BzCustomerServiceImpl implements IBzCustomerService 
{
    @Autowired
    private BzCustomerMapper bzCustomerMapper;

    /**
     * 查询客户
     * 
     * @param customerId 客户主键
     * @return 客户
     */
    @Override
    public BzCustomer selectBzCustomerByCustomerId(Long customerId)
    {
        return bzCustomerMapper.selectBzCustomerByCustomerId(customerId);
    }

    /**
     * 查询客户列表
     * 
     * @param bzCustomer 客户
     * @return 客户
     */
    @Override
    public List<BzCustomer> selectBzCustomerList(BzCustomer bzCustomer)
    {
        return bzCustomerMapper.selectBzCustomerList(bzCustomer);
    }

    /**
     * 新增客户
     * 
     * @param bzCustomer 客户
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBzCustomer(BzCustomer bzCustomer)
    {
        int rows = bzCustomerMapper.insertBzCustomer(bzCustomer);
        insertBzGoods(bzCustomer);
        return rows;
    }

    /**
     * 修改客户
     * 
     * @param bzCustomer 客户
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBzCustomer(BzCustomer bzCustomer)
    {
        bzCustomerMapper.deleteBzGoodsByCustomerId(bzCustomer.getCustomerId());
        insertBzGoods(bzCustomer);
        return bzCustomerMapper.updateBzCustomer(bzCustomer);
    }

    /**
     * 批量删除客户
     * 
     * @param customerIds 需要删除的客户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBzCustomerByCustomerIds(Long[] customerIds)
    {
        bzCustomerMapper.deleteBzGoodsByCustomerIds(customerIds);
        return bzCustomerMapper.deleteBzCustomerByCustomerIds(customerIds);
    }

    /**
     * 删除客户信息
     * 
     * @param customerId 客户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBzCustomerByCustomerId(Long customerId)
    {
        bzCustomerMapper.deleteBzGoodsByCustomerId(customerId);
        return bzCustomerMapper.deleteBzCustomerByCustomerId(customerId);
    }

    /**
     * 新增商品信息
     * 
     * @param bzCustomer 客户对象
     */
    public void insertBzGoods(BzCustomer bzCustomer)
    {
        List<BzGoods> bzGoodsList = bzCustomer.getBzGoodsList();
        Long customerId = bzCustomer.getCustomerId();
        if (StringUtils.isNotNull(bzGoodsList))
        {
            List<BzGoods> list = new ArrayList<BzGoods>();
            for (BzGoods bzGoods : bzGoodsList)
            {
                bzGoods.setCustomerId(customerId);
                list.add(bzGoods);
            }
            if (list.size() > 0)
            {
                bzCustomerMapper.batchBzGoods(list);
            }
        }
    }
}
